package com.yuju.common.core.dao;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.collect.Maps;
import com.yuju.common.Page;
import com.yuju.common.PageModel;
import com.yuju.common.core.convert.BeanMapper;
import com.yuju.persist.base.mapper.BaseMapper;
import com.yuju.util.GenericsUtil;

import net.sf.cglib.beans.BeanMap;

public abstract class AbstractBaseDao<M, P, PK> implements IBaseDao<M, P, PK> {
	@Autowired
	private BeanMapper beanMapper;
	
	public abstract BaseMapper<P, PK> getMapper();
	
	@Override
	public int count(Map<Object, Object> map) {
		return getMapper().selectCountByMap(map);
	}

	public List<P> getList(Map<Object, Object> map) {
		return getMapper().selectListByMap(map);
	}
	
	@Override
	public List<M> list(Map<Object, Object> map) {
		return convertP2M(getMapper().selectListByMap(map));
	}
	
	@Override
	public PageModel list(M model, Integer currentPage, Integer pageSize) {
		PageModel pageModel = new PageModel(currentPage, pageSize);
		// 查询参数
		Map<Object, Object> map = getBeanMap(model, currentPage, pageSize);
		
		Integer total = count(map);
		if (total == null || total == 0) {
			return null;
		}
		// 设置总记录数到返回结果中
		pageModel.setTotal(total);
		
		List<P> list = getList(map);
		// 转换为Model泛型的List
		List<M> modelList = convertP2M(list);
		//List<M> modelList = convert(list);
		
		pageModel.setRows(modelList);
		return pageModel;
	}

	public P getObj(PK id) {
		return getMapper().selectByPrimaryKey(id);
	}

	@Override
	public int save(M m) {
		int rows = getMapper().insert(convert(m));
		return rows;
	}

	@Override
	public int saveSelective(M m) {
		return getMapper().insertSelective(convert(m));
	}

	@Override
	public int update(M m) {
		return getMapper().updateByPrimaryKey(convert(m));
	}

	@Override
	public int updateSelective(M m) {
		return getMapper().updateByPrimaryKeySelective(convert(m));
	}

	@Override
	public int del(PK id) {
		// TODO Auto-generated method stub
		return getMapper().deleteByPrimaryKey(id);
	}
	
	/**
	 * 功能：将bean转换成Map<br/>
	 * 构造分页对象
	 * @author xuehj
	 * @Date 2017年9月13日 下午6:46:20 <br/>
	 */
	public Map<Object, Object> getBeanMap(M m, int currentPage, int rows) {
		Map<Object, Object> map = getBeanMap(m);

		if (rows > 0) {
			Page page = new Page(rows, currentPage);
			map.put("page", page);
		}
		
	    return map;
	}
	
	/**
	 * 功能：将bean转换成Map<br/>
	 *
	 * @author xuehj
	 * @Date 2017年9月13日 下午6:46:20 <br/>
	 */
	public Map<Object, Object> getBeanMap(M m) {
		Map<Object, Object> map = Maps.newHashMap();
	    if (m != null) {
	        BeanMap beanMap = BeanMap.create(m);
	        for (Object key : beanMap.keySet()) {
	        	if (null != beanMap.get(key)) {
	        		map.put(key, beanMap.get(key));
	        	}
	        }
	    }
	    return map;
	}

    /**
     * model 转 entity
     *
     * @param t model
     * @return entity
     */
    protected P convert(M t) {
        if (t == null) {
            return null;
        }
        return (P) beanMapper.map(t, GenericsUtil.getSuperClassGenricType(this.getClass(), 0));
    }

    /**
     * model 转 entity
     *
     * @param t model
     * @return entity
     */
    protected M convertP2M(P t) {
        if (t == null) {
            return null;
        }
        return (M) beanMapper.map(t, GenericsUtil.getSuperClassGenricType(this.getClass(), 0));
    }

    /**
     * 一组 model 转 entity
     *
     * @param data model
     * @return entity集合
     */
    protected List<M> convertP2M(Iterable<P> data) {
        if (data == null) {
            return null;
        }
        Class generic = GenericsUtil.getSuperClassGenricType(this.getClass(), 0);
        return beanMapper.mapAsList(data, generic);
    }

}
